package com.glory.brepda.activity.lanjian;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.BreESDEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

public class Bre3fcCheckESDClothesActivity extends BaseActivity {

    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBCOPType)
    TextView tvBCOPType;
    @BindView(R.id.ivBCOPArrow)
    ImageView ivBCOPArrow;
    @BindView(R.id.flBCOPList)
    FrameLayout flBCOPList;
    @BindView(R.id.tvBCOPOKType)
    TextView tvBCOPOKType;
    @BindView(R.id.ivBCOPOKArrow)
    ImageView ivBCOPOKArrow;
    @BindView(R.id.flBCOPOKList)
    FrameLayout flBCOPOKList;

    @BindView(R.id.etUserName)
    FreeUI_ClearEditText etUserName;
    @BindView(R.id.etDate)
    FreeUI_ClearEditText etDate;
    @BindView(R.id.etJD)
    FreeUI_ClearEditText etJD;
    @BindView(R.id.etNum)
    FreeUI_ClearEditText etNum;
    @BindView(R.id.etOkMan)
    FreeUI_ClearEditText etOkMan;

    private FreeUI_GeneralPop codePop,codePop2;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bre3fc_check_esdclothes;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);

        // ①组件监听的方式
        flBCOPList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flBCOPList.getWidth(), new ArrayList<String>(Arrays.asList("合格","不合格","请重测")), ivBCOPArrow, (ty, i) -> {
                        tvBCOPType.setText(ty);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivBCOPArrow);
                codePop.showAsDropDown(flBCOPList, 0, 0);
            }
        });

        // ①组件监听的方式
        flBCOPOKList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (codePop2 != null) {
                    if (codePop2.isShowing()) {
                        codePop2.dismiss();
                    }
                } else {
                    codePop2 = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flBCOPOKList.getWidth(), new ArrayList<String>(Arrays.asList("合格","不合格")), ivBCOPOKArrow, (ty, i) -> {
                        tvBCOPOKType.setText(ty);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivBCOPOKArrow);
                codePop2.showAsDropDown(flBCOPOKList, 0, 0);
            }
        });

    // 初始化
        initInfoMsg();
    }

    @Override
    protected void initData() {


    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {

        if(StringUtils.isEmpty(getText(etUserName)) || StringUtils.isEmpty(getText(etNum)) || StringUtils.isEmpty(tvBCOPType.getText().toString()) ||
                StringUtils.isEmpty(getText(etOkMan)) || StringUtils.isEmpty(tvBCOPOKType.getText().toString())){
            showToast("必填项不能为空",false);
            return;
        }


        boolean int_flag = Pattern.compile("^-?[1-9]\\d*$").matcher(getText(etNum)).find();

        boolean double_flag = Pattern.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$").matcher(getText(etNum)).find();

        if(!(int_flag || double_flag)){
            showToast("检测值格式异常!",false);
            return;
        }

        // 子丹要求：结果及确认结果都是合格才能提交
        if(!tvBCOPType.getText().toString().equals("合格") || !tvBCOPOKType.getText().toString().equals("合格")){
            showToast("检测合格确认合格才能提交!",false);
            return;
        }

        // 工号，确认人 ：都是扫描工号才能录入
        if(!Pattern.compile("^-?[1-9]\\d*$").matcher(getText(etUserName)).find() || !Pattern.compile("^-?[1-9]\\d*$").matcher(getText(etOkMan)).find()){
            showToast("工号及确认人均需要扫描工号!",false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    etUserName.setText(null);
//                    etDate.setText(null);
//                    etJD.setText(null);
                    etNum.setText(null);
                    etOkMan.setText(null);
                    tvBCOPType.setText(null);
                    tvBCOPOKType.setText(null);
                    initInfoMsg();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "执行异常");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTION", "SAVE");
        map.addProperty("USER_NAME", getText(etUserName));
        map.addProperty("DA", getText(etDate));
        map.addProperty("JD", getText(etJD));
        map.addProperty("NUM", getText(etNum));
        map.addProperty("RESULT", tvBCOPType.getText().toString());  //结果
        map.addProperty("OK_MAN", getText(etOkMan));
        map.addProperty("OK_RESULT", tvBCOPOKType.getText().toString());

        NetUtils.requestPostNet("IF.CHECK_ESD_CLOTHES", "Common", Bre3fcCheckESDClothesActivity.this, map, callBack);

    }


    public void initInfoMsg(){
        // 初始化方法
        FreeUI_EntityCallBack<BaseResponseBean<BreESDEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<BreESDEntity>>
                (new TypeToken<BaseResponseBean<BreESDEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<BreESDEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {

                    etDate.setText(response.body().getResponse().getBody().getDATE());
                    etJD.setText(response.body().getResponse().getBody().getDATE_JD());

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BreESDEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "执行异常");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTION", "QUERY_DATE");
        NetUtils.requestPostNet("IF.CHECK_ESD_CLOTHES", "Common", Bre3fcCheckESDClothesActivity.this, map, callBack);
    }
}